package com.woniuxy.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Many;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.mapping.FetchType;


import com.woniuxy.entity.Perms1;

import com.woniuxy.entity.Role1;

import com.woniuxy.entity.User1;
@Mapper
public interface User1Mapper {
	@Select("select * from t_user1 ")
	public List<User1> all();
	
//	@Select("select * from t_role1 ")
//	public List<Role1> allRoles();
//	@Select("select * from t_perms1 ")
//	public List<Perms1> allPerms();
	@Insert("insert into t_role_perms1 values(#{rid},#{pid})")
	public int addRole1AndPerms1(@Param(value = "rid") int rid,@Param(value = "pid") int pid);
	
	@Insert("insert into t_user_role1 values(#{uid},#{rid})")
	public int addUser1AndRole1(@Param(value = "uid") int uid,@Param(value = "rid") int rid);
	//..............
	@Select("select uid,account,pwd from t_user1 where account = #{account}")
	public User1 findUserByAccount(String account);
	//
	@Select("select uid from t_user1 where account = #{account}")
	@Results({
		@Result(id=true,column="uid",property="uid"),
		@Result(column="uid",property="roles",many=@Many(select="findRolesByUid",fetchType=FetchType.LAZY))
	})
	public User1 findRolesAndPerms(String account);
	//
	@Select("select * from t_user_role1 ur,t_role1 r where ur.rid=r.rid and ur.uid=#{uid}")
	@Results({
		@Result(id=true,column="rid",property="rid"),
		@Result(column="rid",property="perms",many=@Many(select="findPermsByRid",fetchType=FetchType.LAZY))
	})
	public List<Role1> findRolesByUid(int uid);
	//
	@Select("select * from t_role_perms1 rp,t_perms1 p where rp.pid=p.pid and rp.rid=#{rid}")
	public List<Perms1> findPermsByRid(int rid);
	
}
